import CourtsManagement from "./CourtsManagement";
import { getCourts, createCourt, updateCourt, deleteCourt } from "@/lib/courtsService";
import { getCampuses } from "@/lib/campusService";

export default async function CourtsPage() {
  const campuses = await getCampuses({  });
  const courts = await getCourts({  });

  const handleCreateCourt = async (court) => {
    "use server";
    try {
      delete court.id;
      const newCourt = await createCourt(court);
      return { success: true, newCourt };
    } catch (error) {
      console.error("Failed to create court:", error);
      return { message: error.message };
    }
  };

  const handleUpdateCourt = async (court) => {
    "use server";
    try {
      const updatedCourt = await updateCourt(court);
      return { success: true, updatedCourt };
    } catch (error) {
      console.error("Failed to update court:", error);
      return { message: error.message };
    }
  };

  const handleDeleteCourt = async (data) => {
    "use server";
    try {
      const { id } = data;
      await deleteCourt(id);
      return { success: true };
    } catch (error) {
      console.error("Failed to delete court:", error);
      return { message: error.message };
    }
  };

  return (
    <CourtsManagement
      campuses={campuses}
      getCourts={getCourts}
      createCourt={handleCreateCourt}
      updateCourt={handleUpdateCourt}
      deleteCourt={handleDeleteCourt}
    />
  );
}
